<?php

namespace App\Http\Controllers\home;

use Illuminate\Http\Request;
use App\Model\home\register;
use App\Http\Requests;
use Hash;
use DB;
use Mail;
use App\Http\Requests\home\RegisterRequest;
use App\Http\Controllers\Controller;
use Gregwar\Captcha\CaptchaBuilder;
use Session;


class RegisterController extends Controller
{
    /**  加载注册页面
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('/home.register.register');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /** 执行注册操作
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {  
        //两次密码是否相等
        if($request->input('password')==$request->input('repass')){

            $data = $request->except(['_token','repass']);
            $data['password'] = Hash::make($data['password']);
            $data['static'] = 0; //状态等于0 未激活
            $data['token'] = str_random(50);

            //验证码是否一致
            if($data['code']==session('code')){

                unset($data['code']);
                //执行插入
                if($id = DB::table('home_user')->insertGetId($data)){
                    self::send($id,$data['token'],$data['accnum']);
                    echo '<script>alert("注册成功,请去邮箱激活");window.location.href="/home";</script>';
                } else{
                    echo '<script>alert("注册失败");window.location.history.back();</script>';
                }
            }else{
                echo '<script>alert("验证码错误!");window.location.href="/register";</script>';
            }

        }else{

             echo '<script>alert("两次密码不一致!");window.location.href="/register";</script>';
        }

        
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    //验证码
    public function code(){

        //清空缓存区 防止乱码
        ob_clean();
        //生成验证码图片的Builder对象，配置相应属性
        $builder = new CaptchaBuilder;
        //可以设置图片宽高及字体
        $builder->build($width = 120, $height = 40, $font = null);
        //获取验证码的内容
        $phrase = $builder->getPhrase();

        //把内容存入session
        session(['code'=>$phrase]);
        // Session::flash('milkcaptcha', $phrase);
        //生成图片
        header("Cache-Control: no-cache, must-revalidate");
        header('Content-Type: image/jpeg');
        $builder->output();


    }

    //发送邮件
    public static function send($id,$token,$email){
        Mail::send('/home.email.activation', ['id' => $id,'token'=>$token,], function ($m) use ($email) {
            $m->to($email)
              ->subject('激活邮件!');//内容
        });
    }

    //发送验证码
    public function yun(){

        require_once('lib/Ucpaas.class.php');
        //初始化必填
        $options['accountsid']='b3d88b2cb799a346ea328926ef17014f';
        $options['token']='3304db508c7e317ae71993b9fd530a5f';


        //初始化 $options必填
        $ucpass = new Ucpaas($options);

        $appId = "da76d1a8d9d54d51a7b3867f3128f8b6";  //云之讯id
        $to = "18501024440";//给谁发送
        $templateId = "133809";  //几号模板
        $param="服装城,1256,3"; //网站名称,验证码,有效时间

        echo $ucpass->templateSMS($appId,$to,$templateId,$param);
    }


}
